package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.service.NursingProjectPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Transactional
@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanService nursingProjectPlanService;
    /**
     * 查询所有护理计划
     * @return
     */
    @Override
    public Object getAllPlan() {
        return nursingPlanMapper.getAll();
    }

    /**
     * 新增护理计划
     * @param nursingPlanDto
     */
    @Transactional
    @Override
    public void addPlan(NursingPlanDto nursingPlanDto) {
        //新增护理计划
        NursingPlan plan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        plan.setStatus(0);
        nursingPlanMapper.insert(plan);

        /**
         * 批量新增护理项目
         */
        nursingPlanDto.getProjectPlans().forEach(v-> v.setPlanId(plan.getId()));
        nursingProjectPlanService.addList(nursingPlanDto.getProjectPlans());
    }

    /**
     * 条件分页查询
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> getPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        //开启条件分页查询
        PageHelper.startPage(pageNum,pageSize);
        //调用Mapper方法
        Page<NursingPlanVo> byPage = nursingPlanMapper.getPage(name,status);
        //类型转换
        return PageResponse.of(byPage,NursingPlanVo.class);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo getById(Long id) {

        return nursingPlanMapper.getById(id);
    }

    /**
     * 修改
     * @param nursingPlanDto
     */
    @Override
    public void updatePlan(Integer id,NursingPlanDto nursingPlanDto) {
        NursingPlan plan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        // 删除原来的所有护理计划和项目关联表中的Ids
        NursingPlanVo byId = getById(nursingPlanDto.getId());
        List<Long> ids = byId.getProjectPlans().stream().map(NursingProjectPlanVo::getId).collect(Collectors.toList());
        nursingProjectPlanService.deleteByIds(ids);
        if (CollUtil.isNotEmpty(nursingPlanDto.getProjectPlans())) {
            nursingPlanDto.getProjectPlans().forEach(v -> v.setPlanId(nursingPlanDto.getId()));
            nursingProjectPlanService.addList(nursingPlanDto.getProjectPlans());
        }
        nursingPlanMapper.update(plan);

    }

    /**
     * 根据id删除计划
     * @param id
     */
    @Override
    public void delPlan(Integer id) {
        //删除绑定护理项目
        nursingProjectPlanService.deleteById(id);
        nursingPlanMapper.delete(id);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        nursingPlanMapper.status(id,status);
    }
}
